package com.weiaibenpao.demo.chislim.ui.activity.running;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Toast;

import com.orhanobut.logger.Logger;
import com.weiaibenpao.demo.chislim.R;
import com.weiaibenpao.demo.chislim.bean.SportOverInfoBean;
import com.weiaibenpao.demo.chislim.db.entities.RunningRecord;
import com.weiaibenpao.demo.chislim.db.preferences.RunningPreferences;
import com.weiaibenpao.demo.chislim.service.RunningService;
import com.weiaibenpao.demo.chislim.utils.Constant;
import com.weiaibenpao.demo.chislim.utils.RunningUtils;
import com.weiaibenpao.demo.chislim.utils.Utils;

import java.util.List;

import io.objectbox.query.Query;

/**
 * Created by ZJL on 2017/12/20
 */
public class NormalIndoorRunActivity extends BaseRunningActivity {
    public static final String NORMAL_RUNNING = "常规·室内";
    private String mCalorie;
    private String mTotalSteps;

    @Override
    public void init(Bundle savedInstanceState) {
        super.init(savedInstanceState);
        initView();
    }

    private void initView() { //常规室内跑是没有地图的,所以需要隐藏地图和地图上的返回图标  并覆写shouldInitMapByBase 返回false  并使用RunningDataView的  setMapIconVisibility方法隐藏地图图标
        mapView.setVisibility(View.GONE);
        ivHideMap.setVisibility(View.GONE);
        runningDataView.setMapIconVisibility(View.GONE);
    }


    @Override
    protected void putExtraToServiceIntent(Intent serviceIntent) {
        super.putExtraToServiceIntent(serviceIntent);
        serviceIntent.putExtra(RunningService.NEED_SAVE_RECORD, true);
        serviceIntent.putExtra(RunningService.NEED_EXTRA_DATA, true);
        serviceIntent.putExtra(RunningService.NEED_SAVE_STATE, true);
        serviceIntent.putExtra(RunningService.IS_INDOOR_RUNNING,true);
    }

    public static void startSelf(Context context) {
        Intent intent = new Intent(context, NormalIndoorRunActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void restoreDataDisplay() {
        super.restoreDataDisplay();
        showAddToWhiteListNotice();
        mCurrentDistance = RunningPreferences.getInstance().getRunningDistance();
        Bundle bundle = new Bundle();
        bundle.putFloat(RunningService.DISTANCE,RunningPreferences.getInstance().getRunningDistance());
        bundle.putString(RunningService.CALORIE,RunningPreferences.getInstance().getRunningCalorie());
        bundle.putString(RunningService.PACE,RunningPreferences.getInstance().getRunningPace());
        runningDataView.setData(bundle);
        runningDataView.setTime(RunningUtils.getHmsTime((RunningPreferences.getInstance().getRunningTime())));
    }

    @Override
    protected boolean shouldInitMapByBase() {
        return false;
    }

    @Override
    public void onDataUpdated(Bundle data) {
        super.onDataUpdated(data);
        mCalorie = data.getString(RunningService.CALORIE);
        mTotalSteps = data.getString(RunningService.STEP);
    }

    @Override
    protected void uploadRunningData() {
        super.uploadRunningData();
        if(mAlreadyAssembled){
            doUpload();
        }else {
            Query<RunningRecord> query = mRunningRecordBox.query().build();
            List<RunningRecord> records = query.find();
            assembleDataFromDatabase(records);
            mAlreadyAssembled = true;
            doUpload();
        }
    }

    private void assembleDataFromDatabase(List<RunningRecord> records) { //可以考虑挪到BaseRunning
        //室内跑 海拔 步幅 轨迹图都不要
        for(int i=0;i<records.size();i++){
            RunningRecord record = records.get(i);
            String timeStr = String.valueOf(record.getTime());
            mFrequencyPhoto.append(timeStr).append(":").append(record.getFrequency()).append(",");
            if(!TextUtils.isEmpty(record.getKmPace())){            //配速格式为 整公里数：配速：时间，
                mPaceForm.append(record.getKmValue()).append(":").append(record.getKmPace()).append(":").append(timeStr).append(",");
            }
        }
        Logger.e("上传的数据" + "  步频--" + mFrequencyPhoto  + "   配速--" + mPaceForm);
        Logger.e("上传的数据" + "  步频size--" + mFrequencyPhoto.length()  + "   配速size--" + mPaceForm.length());
    }

    private void doUpload() {
        if(isDestroyed()){
            return;
        }
        if(!Utils.isNetWorkConnected()){
            Toast.makeText(this, getString(R.string.network_not_connected), Toast.LENGTH_SHORT).show();
            return;
        }
        mPresenter.postRunData(userId, Constant.SECONDARY_TYPE_NORMAL_INDOOR, Constant.TYPE_NORMAL,String.valueOf(marathonType),"","",0,mCalorie,
                "",String.valueOf(mAltitudePhoto),String.valueOf(mFrequencyPhoto),String.valueOf(mStridePhoto),
                String.valueOf(mPaceForm), String.valueOf(mLocusPhoto),"",0,mTotalTime,mTotalSteps,mCurrentDistance/1000);
    }

    @Override
    public void onUploadDataSuccess(SportOverInfoBean sportInfoBean) {
        super.onUploadDataSuccess(sportInfoBean);
        ShareActivity.startSelf(this,sportInfoBean,NORMAL_RUNNING,"");
        doFinish();
    }

}
